import os
from . import git_op

"""
默往企业版App代码同步
"""

def Get_All_Items():
    items = [
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBVoice.git",
            "item_name": "BBVoice"
        },
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBFoundationModule.git",
            "item_name": "BBFoundationModule"
        },
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBHomepageModule.git",
            "item_name": "BBHomepageModule"
        },
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBIMModule.git",
            "item_name": "BBIMModule"
        },
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBLoginModule.git",
            "item_name": "BBLoginModule"
        },
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBMineModule.git",
            "item_name": "BBMineModule"
        },
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBMomentModule.git",
            "item_name": "BBMomentModule"
        },
        {
            "remote_url": "git@192.168.199.85:beibei/ios/BBRoomModule.git",
            "item_name": "BBRoomModule"
        }
    ]

    return items

def sync_code(git_path, branch="dev"):
    for item in Get_All_Items():
        item_path = git_path + "/" + item["item_name"]
        if os.path.exists(item_path):
            current_branch_name = git_op.git_branch_current(item_path)
            if (current_branch_name != branch):
                print("当前的分支不是需要打包的分支，请确认 %s", current_branch_name)
                return
            git_op.excute_shell('git reset --hard', path=item_path)
            # got pull
            print("exist item: ", item["item_name"])
            git_op.git_pull(git_path=item_path)
        else:
            # git clone
            print("not exist item: ", item["item_name"])
            git_op.git_clone(git_path=git_path, branch=branch, remote_url=item["remote_url"], item_name=item["item_name"])

if __name__ == "__main__":    
    git_path = "/Users/mac/Desktop/GitProjectPackage/MOPro"
    
    sync_code(git_path)
    
    pass